package com.hu.gulimallproduct.project.service.impl;


import com.hu.gulimallcommon.utils.Query;
import com.hu.gulimallproduct.project.dao.PmsAttrAttrgroupRelationDao;
import com.hu.gulimallproduct.project.dao.PmsCategoryDao;
import com.hu.gulimallproduct.project.entity.PmsCategoryEntity;
import com.hu.gulimallproduct.project.service.PmsCategoryBrandRelationService;
import com.hu.gulimallproduct.project.service.PmsCategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.gulimallcommon.utils.PageUtils;



@Service()
public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryDao, PmsCategoryEntity> implements PmsCategoryService {
    @Autowired
    private PmsCategoryBrandRelationService brandRelationService;
    @Autowired
    private PmsAttrAttrgroupRelationDao relationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsCategoryEntity> page = this.page(
                new Query<PmsCategoryEntity>().getPage(params),
                new QueryWrapper<PmsCategoryEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public List<PmsCategoryEntity> listWithTree() {
        // 查询所有的分类信息，共1124条
        List<PmsCategoryEntity> categoryEntities = baseMapper.selectList(null);

        // 查找一级分类，即parent_cid为0的分类
        List<PmsCategoryEntity> roots = categoryEntities
                .stream()
                .filter(e -> e.getParentCid() == 0)
                .sorted(Comparator.comparingInt(e -> (e.getSort() == null ? 0 : e.getSort())))
                .collect(Collectors.toList());

        // 将分类组织为树形，即把父类的所有直接子分类存放到父类的children属性中
        roots.forEach(root->{
            List<PmsCategoryEntity> children = getChildren(root, categoryEntities);
            root.setChildren(children);
        });


        return roots;
    }

    @Override
    public Long[] selectCatelogPath(Long catelogId) {
        List<Long> path = new ArrayList<>();
        List<Long> categoryPath = this.findCategoryPath(catelogId, path);
        return categoryPath.toArray(new Long[categoryPath.size()]);
    }

    @Override
    public void updateCascade(PmsCategoryEntity pmsCategory) {
        this.updateById(pmsCategory);
        //修该关联表冗余的数据
        if(StringUtils.isNotEmpty(pmsCategory.getName())){
            brandRelationService.updateCategory(pmsCategory.getCatId(),pmsCategory.getName());
        }
    }

    public List<Long> findCategoryPath(Long catelogId,List<Long> path){
        path.add(catelogId);
        PmsCategoryEntity byId = this.getById(catelogId);
        if(byId.getParentCid()!=0){
            findCategoryPath(byId.getParentCid(),path);
        }
        Collections.reverse(path);
        return path;
    }

    /**
     * 使用递归的方式把三级分类组织为树形数据结构
     * @param root
     * @param categoryEntities
     * @return
     */
    private List<PmsCategoryEntity> getChildren(PmsCategoryEntity root, List<PmsCategoryEntity> categoryEntities) {

        return categoryEntities
                .stream()
                .filter(e-> e.getParentCid().equals(root.getCatId()))
                .map(e->{
                    List<PmsCategoryEntity> childrenSub = getChildren(e, categoryEntities);
                    e.setChildren(childrenSub);
                    return e;
                })
                .sorted(Comparator.comparingInt(e -> (e.getSort() == null ? 0 : e.getSort()))).collect(Collectors.toList());


    }



}